<?php
/**
 * Created by PhpStorm.
 * User: songs
 * Date: 2019/12/10
 * Time: 8:49
 */

namespace App\Services\Pay\AlipayAppPack;

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sdk/AopClient.php';
use AopClient;
use AlipayTradeAppPayRequest;
use App\Services\Pay\PayService;

class AlipayApp
{
    /**
     * 创建支付订单
     * $play_type 支付类型
     * $money  支付钱数
     * $uid 用户
     *$desc 描述
     */
    public function create($money, $out_trade_no, $desc)
    {
        $config=PayService::getConfigAlipay();
        $aop = new AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId =$config['app_id'];
        $aop->rsaPrivateKey =$config['merchant_private_key'];/// '请填写开发者私钥去头去尾去回车，一行字符串';
        $aop->format = "json";
        $aop->charset = "UTF-8";
        $aop->signType = "RSA2";
        $aop->alipayrsaPublicKey = $config['alipay_public_key'];
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        $request = new AlipayTradeAppPayRequest();

        $bodyArray=[
            'body'=>$desc,
            'subject'=>$desc,
            'out_trade_no'=>$out_trade_no,
            'timeout_express'=>'30m',
            'total_amount'=>$money,
            'product_code'=>'QUICK_MSECURITY_PAY',
        ];
        $bodyJson=json_encode($bodyArray);

        $notifyUrl =route('pay.alipay.alipayNotifyApp');
      //  echo $notifyUrl;exit;
        $request->setNotifyUrl($notifyUrl);
        $request->setBizContent($bodyJson);
        $response = $aop->sdkExecute($request);
        return $response;
      //  return htmlspecialchars($response);
//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义，实际打印到日志以及http传输不会有这个问题
    //    echo htmlspecialchars($response);//就是orderString 可以直接给客户端请求，无需再做处理。
    }


    public function notify(){
        $config=PayService::getConfigAlipay();
        $aop = new AopClient;
        $aop->alipayrsaPublicKey = $config['alipay_public_key'];
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        return $flag;
    }





}